<?php include("header_liga_required.php"); ?>
<head>
<title>ZT Kalenderparing</title>

<script type="text/javascript" src="/assets/js/dojo-0.4.3-src/dojo.js"
	djConfig="parseOnLoad: true">
</script>
<script type="text/javascript" src="/assets/js/ajax.js"></script>
<script type="text/javascript">
  dojo.require("dojo.widget.ComboBox");
  dojo.require("dojo.parser");
</script>
</head>
<?php
	$connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
	mysql_select_db(DB_CATALOG);
	
	if (isset($_GET['round'])) $round = $_GET['round'];
	if (isset($_GET['dep'])) $dep = $_GET['dep'];
	if (isset($_GET['homeTeam'])) $homeTeam = $_GET['homeTeam'];
	if (isset($_GET['visitTeam'])) $visitTeam = $_GET['visitTeam'];
	
	if (isset($dep) && isset($round) && isset($homeTeam) && isset($visitTeam)) {
		
		// fetching the group id (1, 2A, etc) and assigning the teams to it
		
		$query = "SELECT group_id from zt_group g 
			join zt_tournament t on t.tournament_id = g.tournament_id 
			where t.isCurrent = 1 and g.group_name = '".$dep."'";
	    $result = mysql_query($query, $connect);
	    $row = mysql_fetch_assoc($result);
	    $groupId = $row['group_id'];
	    
	    $query = "SELECT team_id from zt_team te 
			join zt_tournament t on t.tournament_id = te.tournament_id 
			where t.isCurrent = 1 and te.team_name = '".$homeTeam."'";
	    $result = mysql_query($query, $connect);
	    $row = mysql_fetch_assoc($result);
	    $homeTeamId = $row['team_id'];
	    
	    mysql_query("update zt_team set group_id = ".$groupId." 
	    	where team_id = ".$homeTeamId, $connect);
	    
	    $query = "SELECT team_id from zt_team te 
			join zt_tournament t on t.tournament_id = te.tournament_id 
			where t.isCurrent = 1 and te.team_name = '".$visitTeam."'";
	    $result = mysql_query($query, $connect);
	    $row = mysql_fetch_assoc($result);
	    $visitTeamId = $row['team_id'];
	    
	    mysql_query("update zt_team set group_id = ".$groupId." 
	    	where team_id = ".$visitTeamId, $connect);
	    
	    // fetching the calendar id
	    
	    $query = "SELECT calendar_id from zt_calendar g
	    	join zt_tournament t on t.tournament_id = g.tournament_id
	    	where t.iscurrent = 1 and round_nr = ".$round;
	    $result = mysql_query($query, $connect);
	    $row = mysql_fetch_assoc($result);
	    $calendarId = $row['calendar_id'];
	    
	    // inserting the calendar pairing (if not present)
	    
	    $query = "SELECT calendar_id from zt_calendar_pairing
	    	where calendar_id = ".$calendarId." and home_team = ".$homeTeamId.
	    	" and visit_team = ".$visitTeamId;
	    $result = mysql_query($query, $connect);
	    if (mysql_num_rows($result) == 0) {
	    	mysql_query("insert into zt_calendar_pairing (calendar_id, home_team, 
	    	visit_team) values (".$calendarId.",".$homeTeamId.",".$visitTeamId.")",
	    	$connect);
	    }
	    
	}
	
	$result = mysql_query("select c.round_nr, g.group_name, ht.team_name as homeTeam, 
		vt.team_name as visitTeam
		from zt_calendar_pairing cp join zt_calendar c on c.calendar_id = cp.calendar_id
		join zt_team ht on ht.team_id = cp.home_team
		join zt_team vt on vt.team_id = cp.visit_team
		join zt_group g on g.group_id = ht.group_id
		join zt_tournament tr on tr.tournament_id = c.tournament_id
		where tr.iscurrent = 1
		order by c.round_nr desc, g.group_name, ht.team_name");

	$calendarPairings = array();
	while ($row = mysql_fetch_assoc($result)) {
        $calendarPairing = new CalendarPairing;
	    $calendarPairing->round_nr = $row['round_nr'];
	    $calendarPairing->group_name = $row['group_name'];
	    $calendarPairing->home_team = $row['homeTeam'];
	    $calendarPairing->visit_team = $row['visitTeam'];
	    array_push($calendarPairings, $calendarPairing);
	}
?>

<body onLoad="document.cp.round.focus()">

<table height="100%" cellSpacing="0" cellPadding="0" width="100%"
	border="0" bgcolor="#FFFFFF">
	<tr>
		<td id="leftFiller" valign="top" height="100%" rowspan="3"
			bgcolor="#666666">
		<table border="0" cellpadding="0" cellspacing="0" height="100%"
			width="8px">
			<tr>
				<td>&nbsp;</td>
			</tr>
		</table>
		</td>
		<td id="header" valign="top" width="100%">
		<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="40%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;
                          <span class="headerSmall">Huidige versie PLAYER.DBF: <?=CURRENT_PLAYER_DBF?>
                        <?php
                        if (isset($user) && $user->club_id == 100) {
                        ?>
                        <br/>
                        <a class="headerSmall" href="import_player.php">laad andere versie</a>
                        <?php
                        }
                        ?>
                        </span>
                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?> 
                </tr>
			<tr>
				<td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img
					src="../../assets/images/spacer.gif" alt="" height="1" width="1"></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td valign="top">
				<table align="left" cellspacing="10" width="100%">
					<tr>
						<td>
						<div id=dojoDebugOutput></div>
						<span class="blackHeaderLarge">Zilveren Toren <?=CURRENT_ZT_YEAR?>
						- Kalender paringen</span>
						<form name="cp" method="get">
						<table cellpadding="2" cellspacing="0" border="0" width="95%">
							<tr>
								<td align="center"><strong>Ronde</strong></td>
								<td align="center"><strong>Afdeling</strong></td>
								<td align="center"><strong>Thuisploeg</strong></td>
								<td align="center"><strong>Bezoekers</strong></td>
							</tr>
							<tr>
								<td align="center"><input style="text-align:center"  type="text" name="round" size="5" /></td>
								<td align="center"><input style="text-align:center"  type="text" name="dep" size="5" 
									onblur="storeDepartment(this.value, '')"/></td>
								<td>
								<select dojoType="ComboBox"
		                            autoComplete="true"
		                            dataUrl="/dojo/suggest_calendar_team.php?match=%{searchString}"
		                            maxListLength="10"
		                            mode="remote"
		                            name="homeTeam"
		                            style="text-align:center;width:50mm" >
		                        </select>
		                        </td>
		                        <td>
								<select dojoType="ComboBox"
		                            autoComplete="true"
		                            dataUrl="/dojo/suggest_calendar_team.php?match=%{searchString}"
		                            maxListLength="10"
		                            mode="remote"
		                            name="visitTeam"
		                            style="text-align:center;width:50mm" >
		                        </select>
		                        </td>
		                        <td>
		                        <!-- <a href="insert_calendar_pairing.php"><img src="../../assets/images/16x16/shadow/add2.gif" border="0" alt="Voeg paring toe"/> -->
		                        <input type="submit" value="voeg toe" />
		                        </td>
							</tr>
							<tr>
								<td align="center"><strong>Ronde</strong></td>
								<td align="center"><strong>Afdeling</strong></td>
								<td><strong>Thuisploeg</strong></td>
								<td><strong>Bezoekers</strong></td>
							</tr>
							<?php
							foreach ($calendarPairings as $item) {
							?>
							<tr>
								<td align="center" style="border-bottom:#DDDDDD solid 1px"><?=$item->round_nr?></td>
								<td align="center" style="border-bottom:#DDDDDD solid 1px"><?=$item->group_name?></td>
								<td style="border-bottom:#DDDDDD solid 1px"><?=$item->home_team?></td>
								<td style="border-bottom:#DDDDDD solid 1px"><?=$item->visit_team?></td>
							</tr>
							<?php
							}
							?>
						</table>
						</form>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>

		<?php include("footer.php"); ?>